let mongo = require('mongoskin');
let db = mongo.db("mongodb://localhost:27017/wuhu", {native_parser: true});
const collectionName = 'users';

let User = function (obj) {
    this._id = obj._id;
    this.account = obj.account;
    this.nikeName = obj.nikeName;
    this.password = obj.password;
    this.friend = obj.friend;
}
/**
 * 增
 * @param para
 * @returns {Promise}
 */
User.prototype.save = function (para) {
    return new Promise(function (resolve,reject) {
        db.collection(collectionName).insertOne({
            account:para.account,
            nikeName:para.nikeName,
            password:para.password,
            friend : [],
        },function (err,res) {
            db.close();
            if (err) {
                reject(err.message);
            } else {
                let u = new User({
                    _id:res.ops[0]._id,
                    account:res.ops[0].account,
                    nikeName:res.ops[0].nikeName,
                    password:res.ops[0].password,
                    friend:res.ops[0].friend,
                })
                resolve(u);
            }
        })
    })
}
//example
// let u = new User({
//     //_id:res._id,
//     account:'31501337',
//     nikeName:'刘坤',
//     password:'123456',
//     friend:[],
// })
// u.save(u).then(function (data) {
//     console.log(data);
// })
/**
 * 通过网名来获取用户，模糊查询
 * @param nikeName
 * 返回的对象不是User，而是一个Object
 */
User.prototype.findByNikeName = function (nikeName) {
    return new Promise(function (resolve,reject) {
        let pattern = new RegExp(nikeName);
        db.collection(collectionName).find({nikeName:pattern}).toArray(function (err,res) {
            db.close();
            if(err){
                reject(err);
            }else{
                let list = []
                res.forEach(function (t) {
                    let user ={
                        _id: t._id,
                        account:t.account,
                        nikeName: t.nikeName,
                    }
                    list.push(user);
                })
                resolve(list);
            }
        });
    })
}

//example
// let u = new User({});
// u.findByNikeName('刘').then(function (data) {
//     console.log(data);
// });
/**
 * 通过账号来查询，必须确定账号的唯一性
 * @param acc
 * 就是账号
 * @returns {Promise}
 */
User.prototype.findByAccount = function (acc) {
    return new Promise(function (resolve,reject) {
        db.collection(collectionName).findOne({account:acc},function (err,res) {
            db.close();
            if(err){
                reject(err);
            }else{
                resolve(res);
            }
        });
    })
}
//example
// let u = new User({});
// u.findByAccount('31501330').then(function (data) {
//     console.log(data);
// })
User.prototype.findById = function (user) {
    return new Promise(function (resolve,reject) {
        db.collection(collectionName).findById(user._id,function (err,res) {
            db.close();
            if(err){
                reject(err);
            }else{
                res.password = undefined;
                resolve(res);
            }
        })
    })
}
// let user = new User({});
// user.findById({_id:'5b2e61396001160f99df75e3'}).then(function (data) {
//     console.log('data:',data);
// })
/**
 * 通过id来添加朋友
 * @param id
 */
User.prototype.addFriend = function (master,id) {
    return new Promise(function (resolve,reject) {
        let user = new User({_id:id});
        user.findById(user).then(function (data) {
            if(data){
                master.friend.push(user._id);
                db.collection(collectionName).updateById(master._id,{$set:{friend:master.friend}},function (err,res) {
                    db.close();
                    if (err) {
                        reject(err.message);
                    } else {
                        resolve('success');
                    }
                })
            }else{
                reject('no such user')
            }
        })
    })
}
//example
// let master = new User({_id:'5b2e61396001160f99df75e3'});
// master.findById(master).then(function (data) {
//     master = new User(data);
//     master.addFriend(master,master._id).then(function (data) {
//         console.log(data);
//     })
// })

module.exports = User;
